home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Font.java < prev    next >
Text File  |  1998-09-22  |  10KB  |  350 lines

  1. /*
  2.  * @(#)Font.java    1.29 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.FontPeer;
  17.  
  18. /** 
  19.  * A class that produces font objects. 
  20.  *
  21.  * @version     1.29, 07/01/98
  22.  * @author     Sami Shaio
  23.  * @author     Arthur van Hoff
  24.  * @author     Jim Graham
  25.  * @since       JDK1.0
  26.  */
  27. public class Font implements java.io.Serializable {
  28.  
  29.     /* 
  30.      * Constants to be used for styles. Can be combined to mix
  31.      * styles. 
  32.      */
  33.  
  34.     /**
  35.      * The plain style constant.  This style can be combined with 
  36.      * the other style constants for mixed styles.
  37.      * @since JDK1.0
  38.      */
  39.     public static final int PLAIN    = 0;
  40.  
  41.     /**
  42.      * The bold style constant.  This style can be combined with the 
  43.      * other style constants for mixed styles.
  44.      * @since JDK1.0
  45.      */
  46.     public static final int BOLD    = 1;
  47.  
  48.     /**
  49.      * The italicized style constant.  This style can be combined 
  50.      * with the other style constants for mixed styles.
  51.      * @since JDK1.0
  52.      */
  53.     public static final int ITALIC    = 2;
  54.  
  55.     /**
  56.      * Private data.
  57.      */
  58.     transient private int pData;
  59.  
  60.     /** 
  61.      * The platform specific family name of this font. 
  62.      */
  63.     transient private String family;
  64.  
  65.     /** 
  66.      * The logical name of this font. 
  67.      * @since JDK1.0
  68.      */
  69.     protected String name;
  70.  
  71.     /** 
  72.      * The style of the font. This is the sum of the
  73.      * constants <code>PLAIN</code>, <code>BOLD</code>, 
  74.      * or <code>ITALIC</code>. 
  75.      */
  76.     protected int style;
  77.  
  78.     /** 
  79.      * The point size of this font. 
  80.      * @since JDK1.0
  81.      */
  82.     protected int size;
  83.  
  84.     /**
  85.      * The platform specific font information.
  86.      */
  87.     transient FontPeer peer;
  88.  
  89.     /*
  90.      * JDK 1.1 serialVersionUID 
  91.      */
  92.      private static final long serialVersionUID = -4206021311591459213L;
  93.  
  94.     /**
  95.      * Gets the peer of the font.
  96.      * @return  the peer of the font.
  97.      * @since JDK1.1
  98.      */
  99.     public FontPeer getPeer(){
  100.     return peer;
  101.     }
  102.  
  103.     private void initializeFont()
  104.     {
  105.       family = System.getProperty("awt.font." + name.toLowerCase(), name);
  106.       this.peer = Toolkit.getDefaultToolkit().getFontPeer(name, style);
  107.     }
  108.  
  109.     /**
  110.      * Creates a new font with the specified name, style and point size.
  111.      * @param name the font name
  112.      * @param style the constant style used
  113.      * @param size the point size of the font
  114.      * @see Toolkit#getFontList
  115.      * @since JDK1.0
  116.      */
  117.     public Font(String name, int style, int size) {
  118.     this.name = name;
  119.     this.style = style;
  120.     this.size = size;
  121.     initializeFont();
  122.     }
  123.  
  124.     /**
  125.      * Gets the platform specific family name of the font. Use the 
  126.      * <code>getName</code> method to get the logical name of the font.
  127.      * @return    a string, the platform specific family name.
  128.      * @see       java.awt.Font#getName
  129.      * @since     JDK1.0
  130.      */
  131.     public String getFamily() {
  132.     return family;
  133.     }
  134.  
  135.     /**
  136.      * Gets the logical name of the font.
  137.      * @return    a string, the logical name of the font.
  138.      * @see #getFamily
  139.      * @since     JDK1.0
  140.      */
  141.     public String getName() {
  142.     return name;
  143.     }
  144.  
  145.     /**
  146.      * Gets the style of the font.
  147.      * @return the style of this font.
  148.      * @see #isPlain
  149.      * @see #isBold
  150.      * @see #isItalic
  151.      * @since JDK1.0
  152.      */
  153.     public int getStyle() {
  154.     return style;
  155.     }
  156.  
  157.     /**
  158.      * Gets the point size of the font.
  159.      * @return the point size of this font.
  160.      * @since JDK1.0
  161.      */
  162.     public int getSize() {
  163.     return size;
  164.     }
  165.  
  166.     /**
  167.      * Indicates whether the font's style is plain.
  168.      * @return     <code>true</code> if the font is neither 
  169.      *                bold nor italic; <code>false</code> otherwise.
  170.      * @see        java.awt.Font#getStyle
  171.      * @since      JDK1.0
  172.      */
  173.     public boolean isPlain() {
  174.     return style == 0;
  175.     }
  176.  
  177.     /**
  178.      * Indicates whether the font's style is bold.
  179.      * @return    <code>true</code> if the font is bold; 
  180.      *            <code>false</code> otherwise.
  181.      * @see       java.awt.Font#getStyle
  182.      * @since     JDK1.0
  183.      */
  184.     public boolean isBold() {
  185.     return (style & BOLD) != 0;
  186.     }
  187.  
  188.     /**
  189.      * Indicates whether the font's style is italic.
  190.      * @return    <code>true</code> if the font is italic; 
  191.      *            <code>false</code> otherwise.
  192.      * @see       java.awt.Font#getStyle
  193.      * @since     JDK1.0
  194.      */
  195.     public boolean isItalic() {
  196.     return (style & ITALIC) != 0;
  197.     }
  198.  
  199.     /**
  200.      * Gets a font from the system properties list.
  201.      * @param nm the property name
  202.      * @see       java.awt.Font#getFont(java.lang.String, java.awt.Font)
  203.      * @since     JDK1.0
  204.      */
  205.     public static Font getFont(String nm) {
  206.     return getFont(nm, null);
  207.     }
  208.  
  209.     /**
  210.      * Gets the specified font using the name passed in.
  211.      * @param str the name
  212.      * @since JDK1.1
  213.      */
  214.     public static Font decode(String str) {
  215.     String fontName = str;
  216.     int fontSize = 12;
  217.     int fontStyle = Font.PLAIN;
  218.  
  219.     int i = str.indexOf('-');
  220.     if (i >= 0) {
  221.         fontName = str.substring(0, i);
  222.         str = str.substring(i+1);
  223.         if ((i = str.indexOf('-')) >= 0) {
  224.         if (str.startsWith("bold-")) {
  225.             fontStyle = Font.BOLD;
  226.         } else if (str.startsWith("italic-")) {
  227.             fontStyle = Font.ITALIC;
  228.         } else if (str.startsWith("bolditalic-")) {
  229.             fontStyle = Font.BOLD | Font.ITALIC;
  230.         }
  231.         str = str.substring(i + 1);
  232.         }
  233.         try {
  234.         fontSize = Integer.valueOf(str).intValue();
  235.         } catch (NumberFormatException e) {
  236.         }
  237.     }
  238.     return new Font(fontName, fontStyle, fontSize);
  239.     }
  240.  
  241.     /**
  242.      * Gets the specified font from the system properties list.
  243.      * The first argument is treated as the name of a system property to 
  244.      * be obtained as if by the method <code>System.getProperty</code>.  
  245.      * The string value of this property is then interpreted as a font. 
  246.      * <p>
  247.      * The property value should be one of the following forms: 
  248.      * <ul>
  249.      * <li><em>fontname-style-pointsize</em>
  250.      * <li><em>fontname-pointsize</em>
  251.      * <li><em>fontname-style</em>
  252.      * <li><em>fontname</em>
  253.      * </ul>
  254.      * where <i>style</i> is one of the three strings 
  255.      * <code>"BOLD"</code>, <code>"BOLDITALIC"</code>, or 
  256.      * <code>"ITALIC"</code>, and point size is a decimal 
  257.      * representation of the point size. 
  258.      * <p>
  259.      * The default style is <code>PLAIN</code>. The default point size 
  260.      * is 12. 
  261.      * <p>
  262.      * If the specified property is not found, the <code>font</code> 
  263.      * argument is returned instead. 
  264.      * @param nm the property name
  265.      * @param font a default font to return if property <code>nm</code> 
  266.      *             is not defined
  267.      * @return    the <code>Font</code> value of the property.
  268.      * @since     JDK1.0
  269.      */
  270.     public static Font getFont(String nm, Font font) {
  271.     String str = System.getProperty(nm);
  272.     if (str == null) {
  273.         return font;
  274.     }
  275.     return Font.decode(str);
  276.     }
  277.  
  278.     /**
  279.      * Returns a hashcode for this font.
  280.      * @return     a hashcode value for this font.
  281.      * @since      JDK1.0
  282.      */
  283.     public int hashCode() {
  284.     return name.hashCode() ^ style ^ size;
  285.     }
  286.     
  287.     /**
  288.      * Compares this object to the specifed object.
  289.      * The result is <code>true</code> if and only if the argument is not 
  290.      * <code>null</code> and is a <code>Font</code> object with the same 
  291.      * name, style, and point size as this font. 
  292.      * @param     obj   the object to compare this font with.
  293.      * @return    <code>true</code> if the objects are equal; 
  294.      *            <code>false</code> otherwise.
  295.      * @since     JDK1.0
  296.      */
  297.     public boolean equals(Object obj) {
  298.     if (obj instanceof Font) {
  299.         Font font = (Font)obj;
  300.         return (size == font.size) && (style == font.style) && name.equals(font.name);
  301.     }
  302.     return false;
  303.     }
  304.  
  305.     /** 
  306.      * Converts this object to a String representation. 
  307.      * @return     a string representation of this object
  308.      * @since      JDK1.0
  309.      */
  310.     public String toString() {
  311.     String    strStyle;
  312.  
  313.     if (isBold()) {
  314.         strStyle = isItalic() ? "bolditalic" : "bold";
  315.     } else {
  316.         strStyle = isItalic() ? "italic" : "plain";
  317.     }
  318.  
  319.     return getClass().getName() + "[family=" + family + ",name=" + name + ",style=" +
  320.         strStyle + ",size=" + size + "]";
  321.     }
  322.  
  323.  
  324.     /* Serialization support.  A readObject method is neccessary because
  325.      * the constructor creates the fonts peer, and we can't serialize the
  326.      * peer.  Similarly the computed font "family" may be different
  327.      * at readObject time than at writeObject time.  An integer version is 
  328.      * written so that future versions of this class will be able to recognize 
  329.      * serialized output from this one.
  330.      */
  331.  
  332.     private int fontSerializedDataVersion = 1;
  333.  
  334.     private void writeObject(java.io.ObjectOutputStream s)
  335.       throws java.lang.ClassNotFoundException,
  336.          java.io.IOException 
  337.     {
  338.       s.defaultWriteObject();
  339.     }
  340.  
  341.     private void readObject(java.io.ObjectInputStream s)
  342.       throws java.lang.ClassNotFoundException,
  343.          java.io.IOException 
  344.     {
  345.       s.defaultReadObject();
  346.       initializeFont();
  347.     }
  348. }
  349.  
  350.